import Cookie from 'js-cookie'
import { apigetUserInfo } from '@/api'
import Store from '../store'

export const getCookit = (cookieName = 'token') => {
    return Cookie.get(cookieName)
}

// 获取用户信息
export const getUserInfo = () => {
    // 判断token是否存在
    if(getCookit()) {
        apigetUserInfo().then(res => {
            console.log(Store)
            // 保存到vuex中
            const { commit } = Store;
            commit('CHANGE_USER_INFO', res.data)
        }).catch(err => {
            // 登录过期 清空cookie
            Cookie.remove('token')

            // 跳转到登录页面 让用户重新登录
            window.location.href = '/login'
        })
    }
}